#  Copyright (c) 2012 ARM Ltd.  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. The name of the company may not be used to endorse or promote
#     products derived from this software without specific prior written
#     permission.
#
#  THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#  IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
#  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

DESTDIR =
VPATH = @srcdir@ @srcdir@/..
srcdir = @srcdir@
objdir = .
srcroot = $(srcdir)/../..
objroot = $(objdir)/../..

prefix = @prefix@
exec_prefix = @exec_prefix@

host_alias = @host_alias@
target_alias = @target_alias@

bindir = @bindir@
libdir = @libdir@
tooldir = $(exec_prefix)/$(target_alias)

objtype = @objtype@

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@

# Multilib support variables.
# TOP is used instead of MULTI{BUILD,SRC}TOP.
MULTISRCTOP =
MULTIBUILDTOP =
MULTIDIRS =
MULTISUBDIR =
MULTILIBNAME =
MULTIDO = true
MULTICLEAN = true
MULTI_FLAGS_FOR_TARGET = MULTI_DEFAULT_FLAGS
MULTI_DEFAULT_FLAGS = -DSEMIHOST_V2

SHELL =	/bin/sh

CC = @CC@

AS = @AS@
AR = @AR@
LD = @LD@
RANLIB = @RANLIB@

SUBDIRS = @subdirs@

OBJDUMP = `if [ -f ${objroot}/../binutils/objdump ] ; \
	then echo ${objroot}/../binutils/objdump ; \
	else t='$(program_transform_name)'; echo objdump | sed -e $$t ; fi`
OBJCOPY = `if [ -f ${objroot}/../binutils/objcopy ] ; \
	then echo ${objroot}/../binutils/objcopy ; \
	else t='$(program_transform_name)'; echo objcopy | sed -e $$t ; fi`

OBJS = ftruncate.o libcfunc.o syscalls.o truncate.o _exit.o _kill.o
CRT0		= crt0.o
CRT0_INSTALL	= install-crt0

RDIMON_CRT0	= rdimon-crt0${${MULTILIBNAME}}.o
RDIMON_BSP	= librdimon${${MULTILIBNAME}}.a
RDIMON_OBJS	= $(patsubst %,rdimon-%,$(OBJS))
RDIMON_SCRIPTS	= rdimon${${MULTILIBNAME}}.specs \
		  aem-ve${${MULTILIBNAME}}.specs \
		  aem-validation${${MULTILIBNAME}}.specs \
		  aem-v8-r${${MULTILIBNAME}}.specs
RDIMON_INSTALL	= install-rdimon

CFLAGS		= -g

# Host specific makefile fragment comes in here.
@host_makefile_frag@

#
# build a test program for each target board. Just trying to get
# it to link is a good test, so we ignore all the errors for now.
#

all: ${CRT0} ${RDIMON_CRT0} ${RDIMON_BSP} ${RDIMON_SCRIPTS}
	@rootpre=`pwd`/; export rootpre; \
	srcrootpre=`cd $(srcdir); pwd`/; export srcrootpre; \
	for dir in .. ${SUBDIRS}; do \
	  if [ x$$dir != x.. ]; then \
	    if [ -d $$dir ]; then \
	      (cd $$dir; $(MAKE) $(FLAGS_TO_PASS)); \
	    else true; fi; \
	  else true; fi; \
	done
	$(MAKE) $(AM_MAKEFLAGS) all-recursive

#
# here's where we build the test programs for each target
#
.PHONY: test
test:

#
crt0.o: crt0.S
	$(CC) $(CFLAGS_FOR_TARGET) $($(MULTI_FLAGS_FOR_TARGET)) $(CFLAGS) $(INCLUDES) -o $@ -c $<

rdimon-crt0${${MULTILIBNAME}}.o: crt0.S
	$(CC) $(CFLAGS_FOR_TARGET) $($(MULTI_FLAGS_FOR_TARGET)) $(CFLAGS) $(INCLUDES) -DARM_RDI_MONITOR -o $@ -c $<

#rdimon-trap.o: trap.S
#	$(CC) $(CFLAGS_FOR_TARGET) $($(MULTI_FLAGS_FOR_TARGET)) $(CFLAGS) $(INCLUDES) -DARM_RDI_MONITOR -o $@ -c $<

rdimon-_exit.o: _exit.c
	$(CC) $(CFLAGS_FOR_TARGET) $($(MULTI_FLAGS_FOR_TARGET)) $(CFLAGS) $(INCLUDES) -DARM_RDI_MONITOR -o $@ -c $<

rdimon-ftruncate.o: ftruncate.c
	$(CC) $(CFLAGS_FOR_TARGET) $($(MULTI_FLAGS_FOR_TARGET)) $(CFLAGS) $(INCLUDES) -DARM_RDI_MONITOR -o $@ -c $<

rdimon-_kill.o: _kill.c
	$(CC) $(CFLAGS_FOR_TARGET) $($(MULTI_FLAGS_FOR_TARGET)) $(CFLAGS) $(INCLUDES) -DARM_RDI_MONITOR -o $@ -c $<

rdimon-libcfunc.o: libcfunc.c
	$(CC) $(CFLAGS_FOR_TARGET) $($(MULTI_FLAGS_FOR_TARGET)) $(CFLAGS) $(INCLUDES) -DARM_RDI_MONITOR -o $@ -c $<

rdimon-syscalls.o: syscalls.c
	$(CC) $(CFLAGS_FOR_TARGET) $($(MULTI_FLAGS_FOR_TARGET)) $(CFLAGS) $(INCLUDES) -DARM_RDI_MONITOR -o $@ -c $<

rdimon-truncate.o: truncate.c
	$(CC) $(CFLAGS_FOR_TARGET) $($(MULTI_FLAGS_FOR_TARGET)) $(CFLAGS) $(INCLUDES) -DARM_RDI_MONITOR -o $@ -c $<

$(RDIMON_BSP): $(RDIMON_OBJS)
	${AR} ${ARFLAGS} $@ $^
	${RANLIB} $@

# Multilib support.
.PHONY:  multi-do all-multi clean-multi install-multi

all-recursive: all-multi
clean-recursive: clean-multi
install-recursive: install-multi

# The $(MAKE) comments below are to enable parallel building.
all-multi:
	$(MAKE) $(AM_MAKEFLAGS) DO=all multi-do

clean-multi:
	$(MAKE) $(AM_MAKEFLAGS) DO=clean multi-do

install-multi:
	$(MAKE) $(AM_MAKEFLAGS) DO=install multi-do

clean mostlyclean:
	rm -f a.out core *.i *.o *-test *.srec *.dis *.x $(SIM_BSP) ${RDIMON_SCRIPTS}
	@rootpre=`pwd`/; export rootpre; \
	srcrootpre=`cd $(srcdir); pwd`/; export srcrootpre; \
	for dir in .. ${SUBDIRS}; do \
	  if [ x$$dir != x.. ]; then \
	    if [ -d $$dir ]; then \
	      (cd $$dir; $(MAKE) $(FLAGS_TO_PASS) clean); \
	    else true; fi; \
	  else true; fi; \
	done
	$(MAKE) $(AM_MAKEFLAGS) clean-recursive

distclean maintainer-clean realclean: clean
	rm -f Makefile config.status *~

.PHONY: install info install-info clean-info
install: ${CRT0_INSTALL} ${RDIMON_INSTALL}
	@rootpre=`pwd`/; export rootpre; \
	srcrootpre=`cd $(srcdir); pwd`/; export srcrootpre; \
	for dir in .. ${SUBDIRS}; do \
	  if [ x$$dir != x.. ]; then \
	    if [ -d $$dir ]; then \
	      (cd $$dir; $(MAKE) $(FLAGS_TO_PASS) install); \
	    else true; fi; \
	  else true; fi; \
	done
	$(MAKE) $(AM_MAKEFLAGS) install-recursive

install-crt0:
	${INSTALL_DATA} ${CRT0} $(DESTDIR)${tooldir}/lib${MULTISUBDIR}/$$x

install-rdimon:
	set -e; for x in ${RDIMON_CRT0} ${RDIMON_BSP}; do ${INSTALL_DATA} $$x $(DESTDIR)${tooldir}/lib${MULTISUBDIR}/$$x; done
	set -e; for x in ${RDIMON_SCRIPTS}; do ${INSTALL_DATA} ${srcdir}/${objtype}$$x $(DESTDIR)${tooldir}/lib${MULTISUBDIR}/$$x; done

# Make copies of the scripts to facilitate in-tree bootstrapping.
${RDIMON_SCRIPTS}: %: ${objtype}%
	cp $< $@

doc:
info:
install-info:
clean-info:

Makefile: Makefile.in config.status @host_makefile_frag_path@
	$(SHELL) config.status

config.status: configure
	$(SHELL) config.status --recheck

include $(srcdir)/../multi-build.in